//
//  HTPlaceView.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/28.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTPlaceView.h"
#import "HTCitiesTools.h"

@interface HTPlaceView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *buttomView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *dismissView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (weak, nonatomic) IBOutlet UIView *backLevelView;

@property (nonatomic,strong) HTCitiesTools *citiesTools;

@property (nonatomic,strong) NSArray *provincesData;
@property (nonatomic,strong) NSArray *citiesData;
@property (nonatomic,strong) NSArray *areaData;

@property (nonatomic,strong) NSArray *dataSource;

@property (nonatomic,assign) NSInteger levelCount;


@end

@implementation HTPlaceView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self = [[[NSBundle mainBundle] loadNibNamed:@"HTPlaceView" owner:self options:nil] lastObject];
    if (self) {
        self.frame = frame;
        [self setupSub];
        [self initData];
    }
    return self;
}
- (void)setPlaceViewType:(HTPlaceViewType)placeViewType{
    _placeViewType = placeViewType;
    if (placeViewType == HTPlaceViewTypeOther) {
        self.titleLabel.text = @"选择公司地址";
    }
}
- (void)setupSub{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    self.dismissView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [self.dismissView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackLevel:)];
    [self.backLevelView addGestureRecognizer:tap1];
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.flowLayout.itemSize = CGSizeMake((kHTScreenWidth - kWidth(40))/4, kWidth(44));
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(kWidth(20), kWidth(20), kWidth(20), kWidth(20));
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTLineCollectionCell" bundle:nil] forCellWithReuseIdentifier:LineCollectionCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
}
- (IBAction)didClickCancleBtn:(id)sender {
    [self dismiss];
}

-(void)dismiss {
    if (self.blockCallBack) {
        self.blockCallBack();
    }
}

#pragma mark --- collectionViewDeleagate &dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTLineCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LineCollectionCell forIndexPath:indexPath];
    cell.areaModel = self.dataSource[indexPath.row];
    if (indexPath.row == 0) {
         cell.state = 3;
    }else if (indexPath.row < 4){
         cell.state = 1;
    }else if (indexPath.row % 4 == 0){
        cell.state = 2;
    }else{
        cell.state = 0;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HTAreaModel *model = self.dataSource[indexPath.row];
    NSInteger currentIndex = indexPath.row;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoiceItemWithModel:index:PlaceViewType:)]) {
        [self.delegate didChoiceItemWithModel:model index:currentIndex PlaceViewType:self.placeViewType];
    }
    switch (model.areaType) {
        case HTAreaTypeNo:
        {
            
        }
            break;
        case HTAreaTypeProvince:
        {
            self.provinceString = [self.provincesData[currentIndex] title];
            self.citiesData = [self.citiesTools loadCitiesDataWithIndex:currentIndex -1];
            self.dataSource = self.citiesData;
            self.levelCount = 1;
        }
            break;
        case HTAreaTypeDirectCity:
        {
            self.provinceString = [self.provincesData[currentIndex] title];
            self.cityString = @"";
            self.citiesData =[self.citiesTools loadCitiesDataWithIndex:currentIndex - 1];
            self.areaData =[self.citiesTools loadAreasDataWithIndex:currentIndex - 1];
            self.dataSource = self.areaData;
            self.levelCount = 1;
        }
            break;
        case HTAreaTypeCity:
        {
            self.cityString = [self.citiesData[currentIndex] title];
            self.areaData =[self.citiesTools loadAreasDataWithIndex:currentIndex - 1];
            self.dataSource = self.areaData;
            self.levelCount = 2;
        }
            break;
        case HTAreaTypeArea:
        {
            
        }
            break;
        default:
            break;
    }
    [collectionView reloadData];
   
}

#pragma mark -- 数据处理
- (void)initData{
    self.citiesTools = [HTCitiesTools shareInstance];
    self.provincesData = [self.citiesTools loadProvincesData];
    self.dataSource = self.provincesData;
    self.levelCount = 0;

}
//点击了返回上一级
- (void)tapBackLevel:(UITapGestureRecognizer *)sender{
    if (self.levelCount == 0) {
        self.levelCount = 0;
        return;
    }else if (self.levelCount == 1){
        self.levelCount = 0;
        self.dataSource = self.provincesData;
        [self.collectionView reloadData];
        self.cityString = @"";
        self.provinceString = @"";
    }else if (self.levelCount == 2){
        self.dataSource = self.citiesData;
        [self.collectionView reloadData];
        self.cityString = @"";
        self.levelCount = 1;
    }
}
- (void)setLevelCount:(NSInteger)levelCount{
    _levelCount = levelCount;
    if (levelCount == 0) {
        self.backLevelView.hidden = YES;
    }else{
        self.backLevelView.hidden = NO;
    }
}
@end
